#!/usr/bin/env python
#coding:utf-8
import urllib2,re
import urllib,sys

class Weather:
    """Class to get up-to-date weather information for your location"""

    def __init__( self, city ):
        """initializing the city and the url received"""    
        self.setCity(city)
        self.setUrl(city)


    def setCity( self, city ):
        """function to set the city"""
        self.__city=city


    def setUrl( self, city ):
        """function to set the url"""       
        url_city=urllib2.quote(city)
        self.__url='http://api.liqwei.com/weather/utf8/?city='+url_city


    def getCity( self ):
        """function to return the city"""
        return self.__city    


    def getPage( self ):
        """function to get the content of the web page.return the string page content"""
        url=self.__url
        retry=2
        for tim in range(retry):
            try:
                page = urllib2.urlopen(url)
                break
            except:
                pass
        page_content = page.read()
        page.close()          
        return page_content


    def getWeather(self):
        """function get the info what we exactly needed from the web page.return a string reply"""
        page_content=self.getPage() # Get the content of the result web page from api.liqwei.com
        contents=page_content.split('<br/>')
        if(len(contents)==1):
            return ["Oops,你查询的城市不存在或输入格式有误，请检查下或输入'天气'获取帮助"]
        else:
            flag=[" 今天:"," 明天:"," 后天:"]
            count=0
            respon='['+self.__city+'天气情况]'
            for result in contents:
                respon=respon+result.replace(self.__city+',',flag[count])
                count=count+1
                respon=respon+' '             
            return [respon]                   

      

    